//
//  DKOrderRepayViewController.m
//  DanKU
//
//  Created by EDY on 2023/11/6.
//

#import "DKOrderRepayViewController.h"
#import "DKOderRepayTopView.h"
#import "DKOrderDetailsFootView.h"
#import "DKOrderDetailsCell.h"
#import "DKPublicHttp.h"
#import "DKOrderListModel.h"
#import "DKOrderDetailModel.h"

@interface DKOrderRepayViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)DKOrderDetailModel *dataSource;

@end

@implementation DKOrderRepayViewController

-(void)viewWillAppear:(BOOL)animated{
    [self makeNavBack];
}


-(void)setDateSouce:(DKOrderRecordListModel *)dateSouce{
    _dateSouce = dateSouce;
    if (_dateSouce.status == 180) {//预期订单详情
        [DKPublicHttp apiGetOrderDetailAPI:@{@"orderNo":[NSString stringWithFormat:@"%@",dateSouce.orderNo],@"pageType":@"2"} complete:^(DKOrderDetailModel * _Nonnull orderlist) {
            self.dataSource = orderlist;
            [self.tableView reloadData];
        }];
    }else{
        [DKPublicHttp apiGetOrderDetailAPI:@{@"orderNo":[NSString stringWithFormat:@"%@",dateSouce.orderNo],@"pageType":@"2"} complete:^(DKOrderDetailModel * _Nonnull orderlist) {
            self.dataSource = orderlist;
            [self.tableView reloadData];
        }];
    }
} 

-(void)reportData{
    [DKPublicHttp apiUserBrhavRecordForAPI:@{@"loanOrderId":[NSString stringWithFormat:@"%@",self.dataSource.orderNo]} withPageName:DK_RepaymentWay withAction:DK_ActionEnter complete:^{
            
    } fail:^{
            
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =  DK_Hex(@"#FAFAFA");
    [self creatUI];
    [self reportData];
//    [self requestOrderDetail]
}

-(void)creatUI{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(DK_SafeAreaNav_H);
    }];
}
//- (void)requestOrderDetail{
//    if (_dateSouce.status == 180) {//预期订单详情
//        [DKPublicHttp apiGetOrderDetailAPI:@{@"orderNo":[NSString stringWithFormat:@"%@",_dateSouce.orderNo],@"pageType":@"2"} complete:^(DKOrderDetailModel * _Nonnull orderlist) {
//            self.dataSource = orderlist;
//            [self.tableView reloadData];
//        }];
//    }else{
//        [DKPublicHttp apiGetOrderDetailAPI:@{@"orderNo":[NSString stringWithFormat:@"%@",_dateSouce.orderNo],@"pageType":@"1"} complete:^(DKOrderDetailModel * _Nonnull orderlist) {
//            self.dataSource = orderlist;
//            [self.tableView reloadData];
//        }];
//    }
//}
-(UITableView*)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = DK_Hex(@"#FAFAFA");
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.bounces = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[DKOrderDetailsCell class] forCellReuseIdentifier:@"DKOrderDetailsCell"];
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.pairs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DKOrderDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DKOrderDetailsCell" forIndexPath:indexPath];
    cell.PairsSoure = self.dataSource.pairs[indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 321+40-50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    DKOderRepayTopView *headView = [[DKOderRepayTopView alloc] initWithFrame:CGRectMake(0, 0,DK_SCREEN_W , 321+40-50)];
    headView.dataSoure = self.dataSource;
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 240;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    DKOrderDetailsFootView *headView = [[DKOrderDetailsFootView alloc] initWithFrame:CGRectMake(0, 0,DK_SCREEN_W , 240)];
    headView.dataSoure = self.dataSource;
    return headView;
}


@end

